//
// Created by tao on 24-9-17.
//

#include "../Inc/Block.h"

#include <oled.h>
#include <random.h>
#include <stdio.h>
#include <stdlib.h>

static unsigned int _block_speed_cnf = 1;


/**
 * @Desc 为 block 随机生成gap 位置y【10-25】和 gap 大小【20-35】
 * @param block
 */
static void randomGapByBlock(Block *block) {
    block->gap_y = rand_range(10, 25);
    block->gap_size = rand_range(20, 35);
}

void completeBlockInit(Block *block) {
    randomGapByBlock(block);
}


Bool _isBlockReBuild(const Block *block) {
    const int x = block->x + block->w;
    if (x < 0) {
        return true;
    }
    return false;
}

void block_iterator(Block *block) {
    block->x -= _block_speed_cnf;
    if (_isBlockReBuild(block)) {
        block->x = BLOCK_X_INIT;
        randomGapByBlock(block);
    }
}


void OLED_DrawBlock(const Block *block) {
    int w = block->w, x = block->x;
    if (x < 0) {
        w -= -x;
        x = 0;
    }
    if (x >= 128) {
        return;
    }
    if(x+w >= 128) {
        w = 128 - x;
    }
    OLED_DrawFilledRectangle(x, BLOCK_Y, w, block->gap_y, OLED_COLOR_NORMAL);
    OLED_DrawFilledRectangle(x, block->gap_y + block->gap_size, w,
                             BLOCK_H - block->gap_y - block->gap_size, OLED_COLOR_NORMAL);
    //调试用
    // char gapMsg[3];
    // sprintf(gapMsg, "%d", block->gap_size);
    // OLED_PrintASCIIString(block->x, 0, gapMsg, &afont8x6, OLED_COLOR_NORMAL);
    // sprintf(gapMsg, "%d", block->gap_y);
    // OLED_PrintASCIIString(block->x, 50, gapMsg, &afont8x6, OLED_COLOR_NORMAL);
}
